<template>
<el-dialog :title="!dataForm.id ? '新建' :  isDetail ? '详情':'编辑'"
           :close-on-click-modal="false" append-to-body
           :visible.sync="visible" class="JNPF-dialog JNPF-dialog_center" lock-scroll
           width="600px">
<el-row :gutter="15" class="">
<el-form ref="elForm" :model="dataForm" :rules="rules" size="small" label-width="100px" label-position="right" >
    <template v-if="!loading">
            <el-col :span="24" >
                <el-form-item  label="来访事由"  prop="visitname" >
                    <el-input    v-model="dataForm.visitname" placeholder="请输入"  :maxlength="200"  clearable  :style='{"width":"100%"}'></el-input>
                    <el-input   v-model="dataForm.id" :maxlength="200"  clearable  :style='{"width":"100%"}' v-show="false"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24" >
                <el-form-item  label="来访单位"  prop="visitcompany" >
                    <el-input    v-model="dataForm.visitcompany" placeholder="请输入"  clearable  :style='{"width":"100%"}'></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24" >
                <el-form-item  label="来访人员姓名" prop="visituser" >
                    <el-input    v-model="dataForm.visituser" placeholder="请输入"  clearable  :style='{"width":"100%"}'> </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24" >
                <el-form-item  label="身份证"  prop="intime" >
                    <el-input    v-model="dataForm.idcard" placeholder="请输入"  clearable  :style='{"width":"100%"}'></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24" >
                <el-form-item  label="来访者电话"  prop="mobile" >
                    <el-input    v-model="dataForm.mobile" placeholder="请输入"  clearable  :style='{"width":"100%"}'></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24" >
                <el-form-item  label="车牌号" prop="outtime" >
                    <el-input    v-model="dataForm.carnumber" placeholder="请输入"  :maxlength="20"  clearable  :style='{"width":"100%"}'></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24" >
                <el-form-item  label="被访者姓名"  prop="calluer" >
                    <el-input    v-model="dataForm.calluer" placeholder="请输入" v-on:input ="inputFunc()" clearable :style='{"width":"100%"}'></el-input>
                    <el-table :data="gridData" v-if="usrdepflag" @row-click="seleusrdpt">
                        <el-table-column width="150" property="F_RealName" label="用户姓名"></el-table-column>
                        <el-table-column width="100" property="F_FullName" label="所属组织" :style='{"width":"100%"}'></el-table-column>
                    </el-table>
                </el-form-item>
            </el-col>
            <el-col :span="24" >
                <el-form-item  label="被访者部门" prop="calldepartid" >
                    <el-input v-model="dataForm.calldepartid" placeholder="请输入"  clearable readonly v-show="false" :style='{"width":"100%"}'></el-input>
                    <el-input v-model="dataForm.calldepartnam" placeholder="请输入"  clearable readonly :style='{"width":"100%"}'></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24" >
                <el-form-item  label="被访者电话"  prop="callmobile" >
                    <el-input    v-model="dataForm.callmobile"placeholder="请输入"  clearable  :style='{"width":"100%"}'></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24" >
                <el-form-item  label="来访时间"  prop="time" >
                    <el-input    v-model="dataForm.time" placeholder="系统自动生成"  readonly ></el-input>
                </el-form-item>
            </el-col>
    </template>
</el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false"> 取 消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail"> 确 定</el-button>
    </span>
    </el-dialog>
</template>
<script>
    import {getUerDeptList} from '@/api/autorequest'
    import request from '@/utils/request'
    import { getDataInterfaceRes } from '@/api/systemData/dataInterface'
    import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
    export default {
        components: {},
        props: [],
        data() {
            return {
                vshow: false,
                visible: false,
                loading: false,
                isDetail: false,
                usrdepflag: false,
                gridData: [],
                dataForm: {
                    id: '',
                    visitname : '',
                    visitcompany : '',
                    visituser : '',
                    idcard : '',
                    mobile : '',
                    carnumber : '',
                    callusrid : '',
                    calluer : '',
                    calldepartid : '',
                    calldepartnam: '',
                    callmobile : '',
                    time : "",
                },
            rules:
            {
                            visitname: [
                                {
                                    required: true,
                                    message: '请输入',
                                    trigger: 'blur'
                                },
                        ],
                            visitcompany: [
                                {
                                    required: true,
                                    message: '请输入',
                                    trigger: 'blur'
                                },
                        ],
                            visituser: [
                                {
                                    required: true,
                                    message: '请输入',
                                    trigger: 'blur'
                                },
                        ],
                            idcard: [
                                {
                                    required: true,
                                    message: '请输入身份证',
                                    trigger: 'blur'
                                },
                                        {
                                            pattern: /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
                                            message: '请输入正确的身份证号码',
                                            trigger: 'blur'
                                        },
                        ],
                            mobile: [
                                {
                                    required: true,
                                    message: '请输入',
                                    trigger: 'blur'
                                },
                                        {
                                            pattern: /^1[3456789]\d{9}$|^0\d{2,3}-?\d{7,8}$/,
                                            message: '请输入正确的联系方式',
                                            trigger: 'blur'
                                        },
                        ],
                            calluer: [
                                {
                                    required: true,
                                    message: '请输入',
                                    trigger: 'blur'
                                },
                        ],
                            calldepartid: [
                                {
                                    required: true,
                                    message: '请输入',
                                    trigger: 'blur'
                                },
                        ],
                            callmobile: [
                        ],
            }

        }

        },
        computed: {},
        watch: {
            //'dataForm.calluer'(newVal,oldVal){
             //   if(newVal != oldVal && newVal != ""){
              //  　	var appName = newVal.split("-")[0]
              //  　	this.dataForm.calldepartid =appName
		      //  }
            //}
        },
        created() {
        },
        mounted() {},
        methods: {
           inputFunc(){
                if(this.dataForm.calluer.length>=2){
                    console.log(this.dataForm.calluer)
                    const dat = new FormData()
                    dat.append('calluer', this.dataForm.calluer)
                    getUerDeptList(this.dataForm.calluer).then(res => {
                        console.log(res.data)
                        const usrdpt = res.data
                        let len = usrdpt.length
                        if(len>0){
                            this.usrdepflag = true
                            this.gridData = res.data
                        }

                    })

                }
           },
           seleusrdpt(row){
                console.log(row)
                this.dataForm.calldepartid = row.F_ID
                this.dataForm.calluer=row.F_RealName
                this.dataForm.callusrid=row.USERID
                this.dataForm.calldepartnam=row.F_FullName
                this.usrdepflag = false
                this.gridData = []

           },
            clearData(data){
                for (let key in data) {
                    if (data[key] instanceof Array) {
                        data[key] = [];
                    } else if (data[key] instanceof Object) {
                        this.clearData(data[key]);
                    } else {
                        data[key] = "";
                    }
                }
            },
            init(id, isDetail) {
                this.gridData=[]
                this.dataForm.id = id || 0;
                this.visible = true;
                this.isDetail = isDetail || false;
                this.$nextTick(() => {
                    this.$refs['elForm'].resetFields();
                    if(this.dataForm.id){
                        this.loading = true
                        request({
                            url: '/api/example/BusVisit/'+this.dataForm.id,
                            method: 'get'
                        }).then(res => {
                            this.dataInfo(res.data)
                            this.loading = false
                        });
                    }else{
                       this.clearData(this.dataForm)
                    }
                });
                this.$store.commit('generator/UPDATE_RELATION_DATA', {})
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                            this.request()
                    }
                })
            },
            request() {
                var _data =this.dataList()
                if (!this.dataForm.id) {
                    request({
                        url: '/api/example/BusVisit',
                        method: 'post',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.$emit('refresh', true)
                            }
                        })
                    })
                }else{
                    request({
                        url: '/api/example/BusVisit/'+this.dataForm.id,
                        method: 'PUT',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.$emit('refresh', true)
                            }
                        })
                    })
                }
            },
            dataList(){
                var _data = JSON.parse(JSON.stringify(this.dataForm));
                return _data;
            },
            dataInfo(dataAll){
                let _dataAll =dataAll
                this.dataForm = _dataAll
            },
        },
    }

</script>
